package com.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.web.file.model.UserFile;
import com.web.file.service.UserFileService;
import com.web.mapper.RegisterMapper;
import com.web.model.Register;
import com.web.service.RegisterService;
import lombok.RequiredArgsConstructor;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Primary
public class RegisterServiceImpl implements RegisterService {

    private final RegisterMapper mapper;
    private final UserFileService userFileService;
    @Override
    public int insert(Register o) throws Exception {
        o.setUpDataTime(new Date());
        o.setCreateTime(new Date());
        return mapper.insert(o);
    }

    @Override
    public int delete(Register o) {
        return mapper.deleteByPrimaryKey(o);
    }

    @Override
    public int update(Register o) {
        o.setUpDataTime(new Date());
        return mapper.updateByPrimaryKey(o);
    }

    @Override
    public Register selectById(long id) {
        Register o = new Register();
        o.setRegisterId(id);
        return mapper.selectByPrimaryKey(o);
    }

    @Override
    public List<Register> selectAll() {
        return mapper.selectAll();
    }

    public Register selectByUId(long id){
        Example example = new Example(Register.class);
        example.createCriteria().andEqualTo("userId",id);
        example.createCriteria().andEqualTo("registerStatus",1);
        List<Register> registers = mapper.selectByExample(example);
        if (registers .size() < 1){
            return null;
        }
        return registers.get(0);
    }

    @Override
    public String saveRegisterPhotoReturnStr(MultipartFile file) throws Exception{
        UserFile userFile = userFileService.uploadFile(file);
        // 生成水印图
       userFileService.watermark(new File(userFile.getFilePath()),new File(userFile.getFilePath()),"一起走小程序");
        return userFileService.getImgBase64(userFile.getId());
    }

    public Map saveRegisterPhotoReturnMap(MultipartFile file) throws Exception{
        UserFile userFile = userFileService.uploadFile(file);
        // 生成水印图
        userFileService.watermark(new File(userFile.getFilePath()),new File(userFile.getFilePath()),"一起走小程序");
        return JSONObject.parseObject(JSON.toJSONString(userFile),Map.class);
    }

    @Override
    public String getRegisterPhotoById(long id) throws Exception {
        return userFileService.getImgBase64(id);
    }

}
